package com.zxl.working.controller;

import com.zxl.working.entity.admin.SysUser;
import com.zxl.working.entity.backend.SellerInfo;
import com.zxl.working.entity.backend.SellerUploadDetail;
import com.zxl.working.service.SellerService;
import com.zxl.working.service.ServiceService;
import com.zxl.working.utils.CommUtil;
import com.zxl.working.utils.HttpUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.hpsf.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.UUID;
@CrossOrigin
@RestController
@RequestMapping("/seller")
public class SellerController {

    @Autowired
    private SellerService sellerService;
    @Autowired
    private ServiceService serviceService;
    @Autowired
    private HttpUtil httpUtil;

    @Resource
    private CommUtil commUtil;

    @Value("${local.filePath}")
    private String filePath;

    @Value("${local.fileUrl}")
    private String fileUrl;

    @Value("${request.url}")
    private String requestUrl;


    @PostMapping("/qryUploadDetails")
    public String qryUploadDetails(HttpServletRequest request, @RequestBody String paramStr){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }
        return sellerService.qryUploadDetails(sellerInfo.getSellerId(),paramStr);
    }

    @PostMapping("/insUpload")
    public String insUpload(HttpServletRequest request, @RequestParam("file") MultipartFile file, SellerUploadDetail sellerUploadDetail){
        try {
            SysUser sysUser = (SysUser) request.getSession().getAttribute("loginUser");
            SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
            if(ObjectUtils.isEmpty(sellerInfo)){
                return commUtil.returnStr(-99,"请重新登录","");
            }
            String filename = System.currentTimeMillis()+file.getOriginalFilename();
            String path = filePath+"/"+filename;
            FileUtils.writeByteArrayToFile(new File(path),file.getBytes());
            sellerUploadDetail.setTranser(fileUrl+filename);
            sellerUploadDetail.setSellerAccountId(sellerInfo.getSellerId());
            
            String resStr = sellerService.insUploadDetails(sysUser, sellerUploadDetail);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
            /*TODO 增加邮件发送*/
            httpUtil.get(requestUrl+"/backend/dict/sendMain?flag=2&name="+sellerInfo.getSellerName() + "&money="+sellerUploadDetail.getInvestAmount() +"&time="+ formatter.format(new java.util.Date()));
            return resStr;
        }catch (Exception e){
            return commUtil.returnFalse(e);
        }

    }

    @PostMapping("/qryInvoice")
    public String qryInvoice(HttpServletRequest request,@RequestBody String paramStr){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }
        return sellerService.qryInvoice(sellerInfo.getSellerId(),paramStr);
    }

    @PostMapping("/qrySuccInvoice")
    public String qrySuccInvoice(HttpServletRequest request,@RequestBody String paramStr){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }
        return sellerService.qrySuccInvoice(sellerInfo.getSellerId(),paramStr);
    }

    @PostMapping("/qryRechargeInfos")
    public String qryRechargeInfos(HttpServletRequest request){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        return sellerService.qryRechargeInfos(sellerInfo.getSellerId());
    }

    @PostMapping("/qryAccounts")
    public String qryAccounts(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        return sellerService.qryAccounts(sellerInfo.getSellerId(),params);
    }

    @PostMapping("/qryAmountTotal")
    public String qryAmountTotal(HttpServletRequest request){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        return sellerService.qryAmountBySellerId(sellerInfo.getSellerId());
    }

    @PostMapping("/qryTradeDetails")
    public String qryTradeDetails(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        return sellerService.qryTradeDetails(sellerInfo.getSellerId());
    }

    @PostMapping("/qryAllService")
    public String qryAllService(HttpServletRequest request){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }
        return serviceService.qryAllService(sellerInfo.getSellerId());
    }

    @PostMapping("/qryServiceType")
    public String qryServiceTypeBySellerId(HttpServletRequest request){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }
        return sellerService.qryServiceTypeBySellerId(sellerInfo.getSellerId());
    }

    @PostMapping("/insFeeSend")
    public String insFeeSend(HttpServletRequest request,@RequestParam("file") MultipartFile file,@RequestParam("serviceId") String serviceId,@RequestParam("serviceName") String serviceName,@RequestParam("reportFile") String reportFile){
        try {
            SysUser sysUser = (SysUser) request.getSession().getAttribute("loginUser");
            SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
            if(ObjectUtils.isEmpty(sellerInfo)){
                return commUtil.returnStr(-99,"请重新登录","");
            }
            String filename = System.currentTimeMillis() + file.getOriginalFilename();

            filename = filename.replaceAll(" ","_");
            filename = filename.replaceAll("&","_");

            String url = fileUrl+filename;
            String path = filePath + "/" + filename;
            FileUtils.writeByteArrayToFile(new File(path), file.getBytes());


            return sellerService.insFee(sysUser,url,path,sellerInfo.getSellerId(),sellerInfo.getSellerName(),serviceName,serviceId,reportFile);
        }catch (Exception e){
            return commUtil.returnFalse(e);
        }
    }

    @PostMapping("/subData")
    public String subData(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }
        return sellerService.subUpData(params,sellerInfo.getSellerName());
    }
    @PostMapping("/updateData")
    public String updateData(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }
        return sellerService.updateData(params);
    }


    @PostMapping("/qryProvideOrder")
    public String qryProvideOrder(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }
        return sellerService.qryProvideOrder(sellerInfo.getSellerId(),params);
    }

    @PostMapping("/qryProvideDetails")
    public String qryProvideDetails(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }
        return sellerService.qryProvideDetails(sellerInfo.getSellerId(),params);
    }
    
    @RequestMapping("/exportAll")
    public String export(HttpServletResponse resp,HttpServletRequest request,String beginDate,String endDate,String orderId,String account,String name){
    	 SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
         if(ObjectUtils.isEmpty(sellerInfo)){
             return commUtil.returnStr(-99,"请重新登录","");
         }
         return sellerService.export(resp,sellerInfo.getSellerId(),beginDate,endDate,orderId,account,name);
    }
    
    

    @PostMapping("/qryNeedBill")
    public String qryNeedBill(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        return sellerService.needBill(sellerInfo.getSellerId(),params);
    }

    //可发放金额
    @PostMapping("/qryCanprovide")
    public String qryCanprovide(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        return sellerService.canProvide(sellerInfo.getSellerId(),params);
    }
    
    @PostMapping("/findByMoney")
    public String findByMoney(HttpServletRequest request){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        return sellerService.findByMoney(sellerInfo.getSellerId());
    }
    
    
	@PostMapping("/upload")
	public String upload(@RequestParam("file") MultipartFile file) {
		if (file.isEmpty()) {
			return "上传失败";
		}
		// 获取文件名
		String fileName = file.getOriginalFilename();

		// 获取文件的后缀名,比如图片的jpeg,png
		String suffixName = fileName.substring(fileName.lastIndexOf("."));

		// 文件上传后的路径
		fileName = UUID.randomUUID() + suffixName;

		File dir=new File(filePath);
		if(!dir.exists()){
			dir.mkdirs();
		}
		File dest = new File(filePath+fileName);
		try {
			file.transferTo(dest);
			return fileName;
		} catch (IllegalStateException e) {
			e.printStackTrace();
			//log.error("seller upload error:{}",e.getMessage(),e);
		} catch (IOException e) {
			e.printStackTrace();
			//log.error("seller upload error:{}",e.getMessage(),e);
		}
		return "上传失败";
	}
    
}
